﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using TWS.Client;


public partial class admin_ChiTietHTTT : BaseAdminPage
{
    protected void btnOK_OnClick(object sender, EventArgs e)
    {
        //Page.Validate();
        //if (!Page.IsValid) return;
        //HoTroTrucTuyenHelper spHP = new HoTroTrucTuyenHelper();
        //HoTroTrucTuyenDataSet spDS = new HoTroTrucTuyenDataSet();
        //HoTroTrucTuyenDataSet.HoTroTrucTuyenRow HoTroTrucTuyenmoi = spDS.HoTroTrucTuyen.NewHoTroTrucTuyenRow();
        //HoTroTrucTuyenmoi.TenHoTroTrucTuyen = txtTenHoTroTrucTuyen.Text;
        //HoTroTrucTuyenmoi.LoaiHoTroTrucTuyen = Convert.ToInt32(ddlLoaiCon.SelectedValue);
        //HoTroTrucTuyenmoi.HangSanXuat = txtHangSanXuat.Text;
        //HoTroTrucTuyenmoi.ChungLoai = txtChungLoai.Text;
        //HoTroTrucTuyenmoi.Gia = Convert.ToInt32(txtGia.Text);
        //////UploadImage();
        //if (txtHinhAnh.Text == string.Empty)
        //{
        //    txtHinhAnh.Text = "noimage.jpg";
        //}
        //HoTroTrucTuyenmoi.HinhAnh = txtHinhAnh.Text;
        //HoTroTrucTuyenmoi.HoTroTrucTuyenMoi = ckHoTroTrucTuyenMoi.Checked;
        //HoTroTrucTuyenmoi.ThuTu = Convert.ToInt32(txtThuTu.Text);
        //HoTroTrucTuyenmoi.NgayDang = DateTime.Now;

        //HoTroTrucTuyenmoi.TongQuan = txtTongQuan.Text;
        //HoTroTrucTuyenmoi.ThongSoKyThuat = txtThongSo.Text;
        //HoTroTrucTuyenmoi.Album = txtAlbum.Text;
        //spDS.HoTroTrucTuyen.AddHoTroTrucTuyenRow(HoTroTrucTuyenmoi);
        //spHP.SaveHoTroTrucTuyen(spDS);
        //RedirectPage(Request.RawUrl, true);
        SaveData();
    }

    private HoTroTrucTuyenDataSet m_HoTroTrucTuyenDataSet;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            try
            {
                string mode = Request.QueryString["mode"];
                bool isNew = false;
                if (mode != "edit")
                {
                    isNew = true;
                }
                string key = Request.QueryString["id"];
                if (!isNew)
                {
                    if (key != null && key != string.Empty)
                    {
                        LoadData(key);
                    }
                }
                else
                {
                    m_HoTroTrucTuyenDataSet = new HoTroTrucTuyenDataSet();

                    HoTroTrucTuyenDataSet.HoTroTrucTuyenRow danhMuc = m_HoTroTrucTuyenDataSet.HoTroTrucTuyen.NewHoTroTrucTuyenRow();
                    danhMuc.ID = -1;
                    danhMuc.NickName = "";
                    danhMuc.SDT = "";
                    m_HoTroTrucTuyenDataSet.HoTroTrucTuyen.AddHoTroTrucTuyenRow(danhMuc);
                    Session[this.Request.Url.AbsolutePath.ToString() + "_" + "Data"] = m_HoTroTrucTuyenDataSet;
                }

            }
            catch (Exception ex)
            {
                //DisplayMesseage(ex.Message);
                this.RedirectPage("pgError.aspx", false);
            }
        }
    }

    private void LoadData(string benhLyId)
    {
        int itemId;
        if (int.TryParse(benhLyId, out itemId))
        {
            HoTroTrucTuyenHelper helper = new HoTroTrucTuyenHelper();
            HoTroTrucTuyenDataSet tempDs = helper.LoadHoTroTrucTuyen(itemId);


            HoTroTrucTuyenDataSet.HoTroTrucTuyenRow danhMuc = tempDs.HoTroTrucTuyen.FindByID(itemId);
            m_HoTroTrucTuyenDataSet = new HoTroTrucTuyenDataSet();
            m_HoTroTrucTuyenDataSet.HoTroTrucTuyen.ImportRow(danhMuc);

            Session[this.Request.Url.AbsolutePath.ToString() + "_" + "Data"] = m_HoTroTrucTuyenDataSet;

            txtTenSanPham.Text = danhMuc.NickName;
            lbTenSanPham.Text = danhMuc.NickName;

            if (!danhMuc.IsSDTNull()) txtSDT.Text = danhMuc.SDT;

            //}
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.RedirectLastVisitedPage();
    }

    private void SaveData()
    {
        try
        {
            string mode = Request.QueryString["mode"];
            m_HoTroTrucTuyenDataSet = (HoTroTrucTuyenDataSet)Session[this.Request.Url.AbsolutePath.ToString() + "_" + "Data"];
            //SetViewMode(canEdit);
            HoTroTrucTuyenDataSet.HoTroTrucTuyenRow danhMuc = m_HoTroTrucTuyenDataSet.HoTroTrucTuyen[0];

            danhMuc.NickName = txtTenSanPham.Text;
            danhMuc.SDT = txtSDT.Text;


            HoTroTrucTuyenHelper helper = new HoTroTrucTuyenHelper();
            helper.SaveHoTroTrucTuyen(m_HoTroTrucTuyenDataSet);
        }
        catch (Exception ex)
        {
            //DisplayMesseage(ex.Message);
            this.RedirectPage("pgError.aspx", false);
        }
        this.RedirectPage("QuanLyHTTT.aspx", false);
    }
}